package com.teatrace.action.produce;

import java.sql.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;
import com.teatrace.model.produce.MaterialPackage;
import com.teatrace.model.produce.Pick;
import com.teatrace.model.produce.TeaTrees;
import com.teatrace.service.produce.ProduceService;
import com.teatrace.service.produce.impl.ProduceServiceImpl;


public class PickAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = Logger.getLogger(PickAction.class);
	
	public ProduceService produceService;
	public HttpServletRequest request ;
	
	public MaterialPackage materialPackage;
	public Pick pick;
	
	
	private String emps;
	private String devices;
	private Date pickTime;
	private int teaTreesId;
	


	public PickAction(){
		produceService  = new ProduceServiceImpl();
		request  = ServletActionContext.getRequest();
	}

	/**
	 * 列出可采摘的茶林
	 * @return
	 */
	public String listCanPickTeaTrees(){
		log.debug("查出可以采摘的茶林");
		try {
			List<TeaTrees> canPickTeaTreeses = produceService.listByCriteria(TeaTrees.class, 0, "pickable",true);
			request.setAttribute("canPickTeaTreeses", canPickTeaTreeses);
			request.setAttribute("msg", "查出可以采摘的茶林");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 增加采摘信息 下一步
	 * @return
	 */
	public String addPickInfoNext(){
		log.debug("增加采摘信息 下一步");
		Set<Employee> pickEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
		Set<Device> pickDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
		pick.setEmps(pickEmpsSet);
		pick.setDevices(pickDevicesSet);
		try {
			TeaTrees teaTrees = (TeaTrees) produceService.loadById(TeaTrees.class, teaTreesId);
			pick.setTeaTrees(teaTrees);
			produceService.add(pick);
			log.debug("新增的采摘信息的id是"+pick.getId());
			HttpSession session = request.getSession();
			session.setAttribute("pick", pick);
			return SUCCESS;
		} catch (Exception e) {
			log.debug(e.getMessage());
			return INPUT;
		}	
	}

	public String addPickInfoCommit(){
		log.debug("关联原料包和采摘信息");
		try {
			HttpSession session = request.getSession();	
			Pick pick  = (Pick) session.getAttribute("pick");
			materialPackage.setPick(pick);
			produceService.add(materialPackage);
			return SUCCESS;
		} catch (Exception e) {
			log.debug(e.getMessage());
			return INPUT;
		}
	}
	
	
	
	
	
	public String getEmps() {
		return emps;
	}

	public void setEmps(String emps) {
		this.emps = emps;
	}

	public String getDevices() {
		return devices;
	}

	public void setDevices(String devices) {
		this.devices = devices;
	}

	public void setPickTime(Date pickTime) {
		this.pickTime = pickTime;
	}

	public Date getPickTime() {
		return pickTime;
	}

	public MaterialPackage getMaterialPackage() {
		return materialPackage;
	}

	public void setMaterialPackage(MaterialPackage materialPackage) {
		this.materialPackage = materialPackage;
	}

	public Pick getPick() {
		return pick;
	}

	public void setPick(Pick pick) {
		this.pick = pick;
	}
	public int getTeaTreesId() {
		return teaTreesId;
	}

	public void setTeaTreesId(int teaTreesId) {
		this.teaTreesId = teaTreesId;
	}
	
}
